import { native } from "cc";
import Ab_NativeClient_bA, { Ab_NativeClientIns_bA } from "../common/NativeClient";

/** iOS端交互接口 */
class Ab_iOSClient_bA extends Ab_NativeClientIns_bA {
	private static Ab_response_cache_bA: Map<string, [abi.Ab_NudityPromise_bA<any>, any]> = new Map();
	private static Ab_used_token_bA: string[] = [];

	public Ab_callStaticMethod_bA(api: string, data: any, ccdata?: any){
		return Ab_iOSClient_bA.Ab_request_bA(api, data, ccdata);
	}

	private static Ab_request_bA(Ab_api_bA: string, Ab_data_bA: any, ccdata?: any){
		let token = abi.Ab_string_bA.Ab_randomToken_bA(5, 36, t=>!Ab_iOSClient_bA.Ab_used_token_bA.includes(t));
		let np = new abi.Ab_NudityPromise_bA();

		Ab_iOSClient_bA.Ab_used_token_bA.push(token);
		Ab_iOSClient_bA.Ab_response_cache_bA.set(token, [np, ccdata]);

		let Ab_request_bA = {
			Ab_cc_token_bA: token,
			Ab_data_bA
		};
		native.reflection.callStaticMethod(
			'Ab_MessageManager_bA',
			'Ab_callNative_bAWithName:data:',
			Ab_api_bA,
			JSON.stringify(Ab_request_bA)
		);
		return {Ab_api_bA, Ab_request_bA, Ab_response_bA: np.Ab_promise_bA};
	}

	private static Ab_response_bA(api: string, json_string: string){
		let response = JSON.parse(json_string);
		let token = response.Ab_cc_token_bA;
		let [np, ccdata] = Ab_iOSClient_bA.Ab_response_cache_bA.get(token);
		Ab_iOSClient_bA.Ab_response_cache_bA.delete(token);
		abi.Ab_array_bA.Ab_remove_bA(Ab_iOSClient_bA.Ab_used_token_bA, token);

		response.Ab_cc_token_bA = ccdata ?? token;
		np.Ab_resolve_bA(response);
	}
}

//Ab_NativeClient_bA.Ab_initClient_bA(new Ab_iOSClient_bA());
//Reflect.set(globalThis ?? window, "Ab_iOSClient_bA", Ab_iOSClient_bA);